<?php
namespace App\Controller;
class ProjectController extends HomeController {

    //用户分组
    public function userType()
    {
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {
             $where['status'] = array('eq',1);
             $list = M('userfz')->where($where)->order('sort asc,id asc')->select();
             $arr = array('code'=>200,'msg'=>'成功','result'=>$list);
             exit(json_encode($arr));
        }
    }

    //用户信息
    public function userInfo()
    {
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {
            $where['status'] = array('eq',1);
            $where['id'] = array('eq',$post['uid']);
            $list = M('usermember')->where($where)->find();
            if(!empty($list)){
				$list['image'] = picture($list['icon']);
                $arr = array('code'=>200,'msg'=>'成功','result'=>$list);
            }else{
                $arr = array('code'=>201,'msg'=>'用户不存在');
            }
            exit(json_encode($arr));
        }
    }

    //首页 和 项目列表接口
    public function projectList()
    {
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {
            if($post['uid']>0){
                if($post['type']==1){
                    //未查看消息
                    $whe['project_id'] = array('in',myProjectId($post['uid']));
                    $whe['user_type'] = array(array('eq','all'),array('eq',modelField($post['uid'],'usermember','user_type')),'or');
                    $whe['status'] = array('eq',1);
                    $whe['uid']    = array('neq',$post['uid']);
                    $whe['create_time'] = thisDay();
                    $count1 = M('user_message')->where($whe)->count();
                    $whe['_string'] = 'FIND_IN_SET('.$post['uid'].',see_person)';
                    $count2 = M('user_message')->where($whe)->count();
                    $result['msg_count'] = $count1-$count2;

                    //首页接口
                    $whd['typeid'] = array('eq',13);
                    $whd['status'] = array('eq',1);
                    $adlist = M('ad')->where($whd)->order('sort asc,id desc')->select();
                    foreach ($adlist as $ke=>$ve){
                        $ad[$ke]['imgurl'] = picture($ve['icon']);
                    }
                    $result['ad'] = $ad;
                    //取出一条评论
                    $whg['_string'] = 'FIND_IN_SET('.$post['uid'].',person_id)';
                    $whg['bhy_comment.status']  = array('eq',1);
                    $whg['bhy_comment.pid']     = array('eq',0);
                    $whg['bhy_comment.content'] = array('neq','');
                    $comment = M('comment')->join('bhy_project as p on p.id=bhy_comment.project_id')->where($whg)->field('bhy_comment.*,p.title,p.person_id')->order('bhy_comment.id desc')->find();
                    $result['comment'] = $comment;
                    //项目列表
                    $where['status']  = array('eq',1);
                    $where['_string'] = 'FIND_IN_SET('.$post['uid'].',person_id)';
                    $list = M('project')->where($where)->order('sort asc,id desc')->limit(10)->select();
                }else{
                    if(trim($post['keywords'])!=''){
                        $where['title']  = array('like','%'.$post['keywords'].'%');
                    }
                    $where['status']  = array('eq',1);
                    $where['_string'] = 'FIND_IN_SET('.$post['uid'].',person_id)';

                    $page = $post['page']>1?($post['page']-1)*$post['num']:0;
                    $list = M('project')->where($where)->order('sort asc,id desc')->limit($page,$post['num'])->select();
                }
                foreach ($list as $ks=>$vs){
                    $list[$ks]['style'] = get_cate($vs['style_id']);
                    $list[$ks]['comment_count'] = comment_count($vs['id']);
                    $list[$ks]['house_type'] = get_cate($vs['huxing_id']);
                    $list[$ks]['imgarr'] = imgarr($vs['imgarr']);
                }
                $result['list'] = $list;
                $arr = array('code'=>200,'msg'=>'成功','result'=>$result);
                exit(json_encode($arr));

            }else{
                $arr = array('code'=>201,'msg'=>'参数错误');
                exit(json_encode($arr));
            }

        }
    }

    //项目工序列表
    public function gongxu()
    {
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {
            $where['p.project_id'] = array('eq',$post['id']);
            $jindu = gongxuJindu($post['id']);
            $list = M('category')->join('bhy_project_date as p on p.gongxu_id=bhy_category.id')->where($where)->field('p.*,bhy_category.title')->order('p.gxsort desc')->select();
            foreach ($list as $ke=>$ve){
				$list[$ke]['update_time'] = $ve['order_time'];
                if($ve['end_status']!=1){
                    $list[$ke]['end_status'] = $ve['gongxu_id']==$jindu['id']?2:0;
                }
            }
            $arr = array('code'=>200,'msg'=>'成功','result'=>$list);
            exit(json_encode($arr));

        }
    }



    //项目详情接口
    public function details()
    {
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {

            //工序详情
            $where['id'] = array('eq',$post['id']);
            $vo = M('project')->where($where)->find();
            $vo['style']      = get_cate($vo['style_id']);
            $vo['house_type'] = get_cate($vo['huxing_id']);
            $vo['imgarr']     = imgarr($vo['imgarr']);

            //工序进度
            $jd = gongxuJindu($post['id']);
            $vo['jin_du']  = $jd['title'];

            $arr = array('code'=>200,'msg'=>'成功','result'=>$vo);
            exit(json_encode($arr));

        }
    }


    //项目评论列表
    public function comment_list(){
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {

            if($post['user_type']>0){
                $whe['u.user_type']   = array('eq',$post['user_type']);
            }
            if($post['long_time']>0){
                $whe['bhy_comment.create_time']   = array('egt',long_time($post['long_time']));
            }
            if($post['sort']==2){
                $sort = 'bhy_comment.id asc';
            }else{
                $sort = 'bhy_comment.id desc';
            }
            $whe['bhy_comment.project_id'] = array('eq',$post['project_id']);
            $result = $this->oneComment($post,$whe,$sort);
            $arr = array('code'=>200,'msg'=>'成功','result'=>$result);
            exit(json_encode($arr));

        }
    }

    //封装一级
    public function oneComment($post,$whe,$sort,$whee){
        $whe['bhy_comment.pid']        = array('eq',0);
        $whe['bhy_comment.status']     = array('eq',1);
        $whe['u.status']               = array('eq',1);
        $page = $post['page']>1?($post['page']-1)*$post['num']:0;
        $comment = M('comment')->join('bhy_usermember as u on u.id=bhy_comment.uid')->where($whe)->field('bhy_comment.*,u.user_type,u.icon,u.username')->order($sort)->limit($page,$post['num'])->select();
         
        foreach ($comment as $ke=>$ve){
			$comment[$ke]['content'] = !empty(trim($ve['content']))?$ve['content']:'';
            $comment[$ke]['shen_fen'] = modelField($ve['user_type'],'userfz','title');
            $comment[$ke]['icon']   = picture($ve['icon']);
            $comment[$ke]['imgarr'] = imgarr($ve['imgarr']);
            $comment[$ke]['imgid']  = $ve['imgarr'];
            $comment[$ke]['title']  = modelField($ve['project_id'],'project','title');
            //二级评论
            $comment[$ke]['son_list'] = $this->twoComment($ve['id'],$whee);
            //判断是否超过5分钟
            if((time()-$ve['create_time'])>=C('COMMENT_OUT_TIME')){
                $comment[$ke]['is_out'] = 1;
            }else{
                $comment[$ke]['is_out'] = 0;
            }
        }
		
        return $comment;
    }

    //封装二级评论列表信息
    public function twoComment($pid,$whee=array()){
        $whee['bhy_comment.pid']        = array('eq',$pid);
        $whee['bhy_comment.status']     = array('eq',1);
        $whee['u.status']               = array('eq',1);
        $twolist = M('comment')->join('bhy_usermember as u on u.id=bhy_comment.uid')->where($whee)->field('bhy_comment.*,u.user_type,u.icon,u.username')->order('bhy_comment.id desc')->select();
        foreach ($twolist as $ke=>$ve){
            $twolist[$ke]['shen_fen'] = modelField($ve['user_type'],'userfz','title');
            $twolist[$ke]['icon']   = picture($ve['icon']);
            $twolist[$ke]['imgarr'] = imgarr($ve['imgarr']);
            //判断是否超过5分钟
            if((time()-$ve['create_time'])>=C('COMMENT_OUT_TIME')){
                $twolist[$ke]['is_out'] = 1;
            }else{
                $twolist[$ke]['is_out'] = 0;
            }
        }
        return $twolist;
    }


    //项目评论 和 签到接口
    public function comment()
    {
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {

            if($post['project_id']>0 && $post['uid']>0){
                 if($post['type']==1){
                     if(trim($post['content'])==''){
                         $arr = array('code'=>201,'msg'=>'请输入评论内容');
                         exit(json_encode($arr));
                     }
                 }
                 $data['project_id'] = $post['project_id'];
                 $data['uid']        = $post['uid'];
                 $data['content']    = trim($post['content'])!=''?$post['content']:'';
                 $data['imgarr']     = trim($post['imgarr'])!=''?$post['imgarr']:'';
                 $data['type']       = 1;
                 $data['pid']        = $post['pid']>0?$post['pid']:0;
                 $data['create_time']= time();
                 $res = M('comment')->add($data);
                 if($res){
                     $arr = array('code'=>200,'msg'=>'提交成功');
                 }else{
                     $arr = array('code'=>201,'msg'=>'提交失败');
                 }
                exit(json_encode($arr));
            }else{
                $arr = array('code'=>201,'msg'=>'缺少参数');
                exit(json_encode($arr));
            }
        }
    }

    //编辑评论
    public function edit_comment(){
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {
              $vo = M('comment')->find($post['id']);
              if((time()-$vo['create_time'])>=300){
                  $arr = array('code'=>201,'msg'=>'5分钟后不能编辑');
                  exit(json_encode($arr));
              }

              if(trim($post['content'])==''){
                    $arr = array('code'=>201,'msg'=>'请输入内容');
                    exit(json_encode($arr));
              }
             $data['content']    = trim($post['content']);
             $data['imgarr']     = trim($post['imgarr'])!=''?$post['imgarr']:'';
             $data['update_time']= time();
             $data['id']         = $post['id'];
             $res = M('comment')->save($data);
             if($res){
                $arr = array('code'=>200,'msg'=>'编辑成功');
             }else{
                $arr = array('code'=>201,'msg'=>'编辑失败');
             }
             exit(json_encode($arr));
        }
    }

    //删除评论
    public function del_comment(){
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {
            $vo = M('comment')->find($post['id']);
            if((time()-$vo['create_time'])>=300){
                $arr = array('code'=>201,'msg'=>'5分钟后不能删除');
                exit(json_encode($arr));
            }
            $res = M('comment')->delete($post['id']);
            if($res){
                $arr = array('code'=>200,'msg'=>'删除成功');
            }else{
                $arr = array('code'=>201,'msg'=>'删除失败');
            }
            exit(json_encode($arr));
        }
    }



    //添加签到日志
    public function add_sign()
    {
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {

              $data['content'] = $post['content'];
              $data['imgarr']  = $post['imgarr'];
              $data['update_time']= time();
              $data['publish_time']= time();
              $data['id']         = $post['comment_id'];
              $res = M('comment')->save($data);
              if($res){
                    $arr = array('code'=>200,'msg'=>'发布成功');
              }else{
                    $arr = array('code'=>201,'msg'=>'发布失败');
              }
              exit(json_encode($arr));
        }
    }

    //签到日志列表
    public function daily_list(){
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {

            if($post['type']==''){
                $arr = array('code'=>201,'msg'=>'缺少参数');
                exit(json_encode($arr));
            }
            $times = timeCondition($post['start_time'],$post['end_time']);
            if($times!=0){
                $whe['bhy_comment.create_time']   = $times;
            }
            if(trim($post['title'])!=''){
                $whe['p.title']   = array('like','%'.$post['title'].'%');
            }
            if($post['type']==1){
                //全部
                $whe['_string']   = 'FIND_IN_SET('.$post['uid'].',person_id)';
            }else if($post['type']==2){
                //我的日志
                $whe['bhy_comment.uid']   = array('eq',$post['uid']);
            }

            if($post['sort']==2){
                $sort = 'bhy_comment.id asc';
            }else{
                $sort = 'bhy_comment.id desc';
            }

           // $whe['bhy_comment.project_id'] = array('eq',$post['project_id']);
            $whe['bhy_comment.pid']        = array('eq',0);
            $whe['bhy_comment.status']     = array('eq',1);
            $whe['u.status']               = array('eq',1);
            $whe['bhy_comment.type']       = array('eq',2);

            $page = $post['page']>1?($post['page']-1)*$post['num']:0;
            $comment = M('comment')->join('bhy_project as p on p.id=bhy_comment.project_id')->join('bhy_usermember as u on u.id=bhy_comment.uid')->where($whe)->field('bhy_comment.*,u.user_type,u.icon,u.username,p.title')->order($sort)->limit($page,$post['num'])->select();

            foreach ($comment as $ke=>$ve){
                $comment[$ke]['shen_fen'] = modelField($ve['user_type'],'userfz','title');
                $comment[$ke]['icon']   = picture($ve['icon']);
                $comment[$ke]['imgarr'] = imgarr($ve['imgarr']);
                $comment[$ke]['imgid']  = $ve['imgarr'];
                $comment[$ke]['can_publish']  = (time()-$ve['create_time'])>7200?0:1; //超过两小时不能发布 
                if($ve['content']==''&&$ve['imgarr']==''){
                    $comment[$ke]['is_publish'] = 0;
                }else{
                    $comment[$ke]['is_publish'] = 1;
                }
                //判断是否超过5分钟
                if((time()-$ve['publish_time'])>=C('COMMENT_OUT_TIME')){
                    $comment[$ke]['is_out'] = 1;
                }else{
                    $comment[$ke]['is_out'] = 0;
                }

            }
            $arr = array('code'=>200,'msg'=>'成功','result'=>$comment);
            exit(json_encode($arr));
        }
    }

    //罚款列表
    public function fakuan(){
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {
            $page = $post['page']>1?($post['page']-1)*$post['num']:0;

            $times = timeCondition($post['start_time'],$post['end_time']);
            if($times!=0){
                $where['bhy_flowater.create_time']  = $times;
            }
            if(trim($post['title'])!=''){
                $where['p.title']   = array('like','%'.$post['title'].'%');
            }
            if($post['type']==2){
                //我的罚款
                $where['bhy_flowater.status'] = array('eq',1);
                $where['bhy_flowater.uid'] = array('eq',$post['uid']);
                $where['bhy_flowater.type'] = array('eq',2);
                $list = M('flowater')->join('bhy_comment as c on c.id=bhy_flowater.return_id')->join('bhy_project as p on p.id=c.project_id')->where($where)->field('bhy_flowater.*,c.project_id')->limit($page,$post['num'])->select();
            }else if($post['type']==1){
                //全部罚款
                $where['p.id'] = array('in',myProjectId($post['uid']));
                $where['bhy_flowater.status'] = array('eq',1);
                $list = M('flowater')->join('bhy_comment as c on c.id=bhy_flowater.return_id')->join('bhy_project as p on p.id=c.project_id')->where($where)->field('bhy_flowater.*,c.project_id')->limit($page,$post['num'])->select();

            }
            foreach ($list as $ks=>$vs){
				$vot = M('usermember')->find($vs['uid']);
				$list[$ks]['shenfen'] = modelField($vot['user_type'],'userfz','title');
				$list[$ks]['username'] = $vot['username'];
                $list[$ks]['title'] = modelField($vs['project_id'],'project','title');
            }


            $arr = array('code'=>200,'msg'=>'成功','result'=>$list);
            exit(json_encode($arr));
        }
    }



    //日志模板接口
    public function board(){
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {

            $where['status'] = array('eq',1);
            $list = M('board')->where($where)->order('sort asc,id desc')->select();
            $arr = array('code'=>200,'msg'=>'成功','result'=>$list);
            exit(json_encode($arr));
        }
    }

    //会员中心我的发布列表
    public function my_publish(){
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {

            $times = timeCondition($post['start_time'],$post['end_time']);
            if($times!=0){
                $whe['bhy_comment.create_time']   = $times;
            }
            if(trim($post['keywords'])!=''){
                $whe['bhy_comment.content']   = array('like','%'.$post['keywords'].'%');
            }
            $whe['bhy_comment.uid']   = array('eq',$post['uid']);


            if($post['sort']==2){
                $sort = 'bhy_comment.id asc';
            }else{
                $sort = 'bhy_comment.id desc';
            }
            $result = $this->oneComment($post,$whe,$sort);
            $arr = array('code'=>200,'msg'=>'成功','result'=>$result);
            exit(json_encode($arr));
        }
    }
    //会员中心我的评论列表
    public function my_comment(){
        $list = file_get_contents('php://input');
        $post = json_decode($list, true);
        $authorization = headerInfo();
        if (check_token($authorization[1])) {

            $times = timeCondition($post['start_time'],$post['end_time']);
            if($times!=0){
                $whe['bhy_comment.create_time']   = $times;
            }

            if($post['keywords']!=''){
                $where['content']   = array('like','%'.$post['keywords'].'%');
            }

            if($post['sort']==2){
                $sort = 'bhy_comment.id asc';
            }else{
                $sort = 'bhy_comment.id desc';
            }
            $where['uid']      = array('eq',$post['uid']);
            $where['status']   = array('eq',1);
            $where['pid']      = array('gt',0);
            $pidarr = M('comment')->where($where)->group('pid')->select();
            $idarr = implode(',',array_column($pidarr,'pid'));
            $whe['bhy_comment.id']   = array('in',$idarr); //一级条件
            $whee['bhy_comment.uid']   = array('eq',$post['uid']); //二级条件

            $result = $this->oneComment($post,$whe,$sort,$whee);
            $arr = array('code'=>200,'msg'=>'成功','result'=>$result);
            exit(json_encode($arr));
        }
    }




   
}